package Rbac.service.array;

import Rbac.domain.dto.UsersDto;
import Rbac.domain.entity.Dept;
import Rbac.domain.entity.Role;
import Rbac.domain.entity.Users;
import Rbac.domain.vo.Result;
import Rbac.domain.vo.UsersVo;
import Rbac.service.IDeptService;
import Rbac.service.IRoleService;
import Rbac.service.IUsersService;

/**
 * 业务用户模块
 */
public class UsersService extends BaseService<Users, UsersDto, UsersVo> implements IUsersService {
    private final IDeptService deptService;
    private final IRoleService roleService;
    private static IUsersService singleton;
    private UsersService(){
        data= new Users[DATA_SIZE+1];
        temp=new UsersVo[DATA_SIZE];
        deptService=DeptService.getInstance();
        roleService=RoleService.getInstance();
    }
    public static IUsersService getInstance(){
        if(singleton==null){
            singleton=new UsersService();
        }
        return singleton;
    }
    @Override
    UsersVo convertToVo(Users users) {
        String deptName="";
        Dept dept=deptService.findById(users.deptId);
        if(dept!=null){
            deptName=dept.name;
        }
        String roleName="";
        Role role=roleService.findById(users.roleId);
        if(role!=null){
            roleName=role.name;
        }
        return new UsersVo(users.getId(),users.username,users.name,users.mobile,users.email,users.deptId,deptName,
                users.roleId,roleName,users.enabled);
    }
    @Override
    Users createEntity(UsersDto dto, int id){
        return new Users(id, dto.username, dto.name, dto.mobile, dto.email, dto.deptId, dto.roleId);
    }
    @Override
    void updateEntity(UsersDto dto, Users user){
        user.username=dto.username;
        user.name=dto.name;
        user.mobile=dto.mobile;
        user.email=dto.email;
        user.deptId=dto.deptId;
        user.roleId=dto.roleId;
    }
    @Override
    public boolean deptInUse(int deptId){
        boolean inUse=false;
        for(Users user : data){
            if(user!=null&&user.deptId==deptId){
                inUse=true;
                break;
            }
        }
        return inUse;
    }
    @Override
    public boolean roleInUse(int roleId){
        boolean inUse=false;
        for(Users user : data){
            if(user!=null&&user.roleId==roleId){
                inUse=true;
                break;
            }
        }
        return inUse;
    }
    @Override
    public Result create(UsersDto dto) {
        if (!deptService.existsById(dto.id)) {
            return Result.failure("创建失败，该部门不存在！");
        }
        if (!roleService.existsById(dto.id)) {
            return Result.failure("创建失败，该角色不存在！");
        }
        return super.create(dto);
    }
    @Override
    public Result update(UsersDto dto, int id){
        if(!deptService.existsById(dto.id)){
            return Result.failure("创建失败，该部门不存在！");
        }
        if(!roleService.existsById(dto.id)){
            return Result.failure("创建失败，该角色不存在！");
        }
        return super.update(dto, id);
    }
    @Override
    public Result changeStatus(int id) {
        if (!existsById(id)) {
            return Result.failure("状态切换失败，数据不存在！");
        }
        data[id].enabled = !data[id].enabled;
        return Result.success(data[id].enabled?"启用成功！":"禁用成功！", null);
    }
}
